package stz.flash.main.flashlight.field;


import net.rim.device.api.i18n.SimpleDateFormat;
import net.rim.device.api.system.Application;
import net.rim.device.api.system.RealtimeClockListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.DateField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.VerticalFieldManager;
import stz.flash.Info;



public class TimePanel extends HorizontalFieldManager {
	
	public static final int FONT_HEIGHT_HOUR_AND_MINUTE = Info.PX_OF_4MM;
	public static final int FONT_HEIGHT_SECOND = Info.PX_OF_2MM;;
	
	private HourAndMinuteField hourAndMinuteField = new HourAndMinuteField();

	//SecondField에는 Style을 적용할수 없어서 Manager로 한번 감쌋음
	private VerticalFieldManager secondFieldManager = new VerticalFieldManager(FIELD_BOTTOM);
	private SecondField secondField = new SecondField();
	
	public TimePanel() {
		super(0);
		setFont();
		
		add(hourAndMinuteField);
		
		secondFieldManager.add(secondField);
		add(secondFieldManager);
	}
	
	private void setFont(){
		hourAndMinuteField.setFont(
				getFont().derive(Font.ITALIC, FONT_HEIGHT_HOUR_AND_MINUTE));
		secondField.setFont(getFont().derive(Font.ITALIC, FONT_HEIGHT_SECOND));
	}

	public boolean isFocusable() {
		return false;
	}

	
	/* 
	 * DateField의 Height는 최소 최대 범위가 있다.
	 * Bold9900 = 57 ~ 63 / Bold9700 = 34 ~ 63 이다 
	 * 폰트크기가 최소값보다도 작아지면 폰트만 따로 작아진다
	 * 폰트크기가 최대값보다 크면 폰드가 더이상 커지지 않는다
	 * 따라서 baseline을 맞추려면 따로 계산을 해야한다 
	 * 내가 만든 코딩은 (폰트크기 63 이상은 권장하지 않음)
	 */ 
	//	protected void sublayout(int i, int j) {
	//	
	//		setPositionChild(hourAndMinuteField, 
	//				0, 
	//				0 
	//				);
	//		layoutChild(hourAndMinuteField, 
	//				hourAndMinuteField.getPreferredWidth(), 
	//				hourAndMinuteField.getPreferredHeight() 
	//				);
	//
	//		
	//		setPositionChild(secondField,
	//				hourAndMinuteField.getLeft() + hourAndMinuteField.getWidth(),
	//				getHourAndMinuteTop()
	//				);
	//		layoutChild(secondField, 
	//				secondField.getPreferredWidth(), 
	//				secondField.getPreferredHeight() 
	//				);
	//		
	//		setExtent(hourAndMinuteField.getPreferredWidth() + secondField.getPreferredWidth(),
	//				hourAndMinuteField.getPreferredHeight());
	//	}
	//
	//	private int getHourAndMinuteTop() {
	//		return getHourAndMinuteBaseline() - getSecondBaseline();
	//	}
	//
	//	// 진짜 baseline의 값을 구함
	//	private int getSecondBaseline() {
	//		return ((secondField.getHeight() - secondField.getFont() .getHeight()) / 2) 
	//				+ secondField.getFont().getBaseline();
	//	}
	//
	//	private int getHourAndMinuteBaseline() {
	//		return ((hourAndMinuteField.getHeight() - hourAndMinuteField.getFont() .getHeight()) / 2) 
	//				+ hourAndMinuteField.getFont().getBaseline();
	//	}
}

abstract class ChameleonDateField extends DateField{
	
	public ChameleonDateField() {
		super();
	}

	protected void paint(Graphics g) {
		g.setColor(FontColor.getFontColor());
		g.setGlobalAlpha(FontColor.GLOBAL_ALPHA);
		super.paint(g);
	}
	
	protected void layout(int i, int j) {
		super.layout(getPreferredWidth(), getPreferredHeight());
	}
}

class HourAndMinuteField extends ChameleonDateField implements RealtimeClockListener {

	public HourAndMinuteField() {
		setFormat(new SimpleDateFormat("hh:mm"));
		setDate(System.currentTimeMillis());
		Application.getApplication().addRealtimeClockListener(this);
	}

	public void clockUpdated() {
		this.setDate(System.currentTimeMillis());
	}
}

class SecondField extends ChameleonDateField {

	public SecondField() {
		autoSecondUpdate();
		setFormat(new SimpleDateFormat(" ss aa"));
	}
	
	private void autoSecondUpdate() {
		new Thread() {
			public void run() {
				while (true) {
					try {
						synchronized (UiApplication.getEventLock()) {
							setDate(System.currentTimeMillis());
						}
						sleep(1000);
					} catch (InterruptedException e) {
					}
				}
			}
		}.start();
	}
	
	protected void paint(Graphics g) {
		super.paint(g);
	}
}
